package com.qfedu.controller;

import com.github.pagehelper.Page;
import com.qfedu.common.JsonResult;
import com.qfedu.pojo.Employee;
import com.qfedu.pojo.VEmployee;
import com.qfedu.service.EmployeeDaoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/employee")
@Controller
public class EmployeeController {
    @Autowired
    private EmployeeDaoService employeeDaoService;

    @RequestMapping("/findall.do")
    @ResponseBody
    public Map<String, Object> findAll(int page, int limit, VEmployee employee) {
        /*if (null!=employee.getName() && employee.getName().equals("")) {
            employee.setName(null);
        }*/
        if (null!=employee.getPhone() && employee.getPhone().equals("")) {
            employee.setPhone(null);
        }
        if (null!=employee.getCardId() && employee.getCardId().equals("")) {
            employee.setCardId(null);
        }
        List<Employee> all = employeeDaoService.findAll(employee, page, limit);
        long total = ((Page) all).getTotal();
        System.out.println(all);
        Map<String, Object> map = new HashMap<>(5);
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", total);
        map.put("data", all);
        return map;
    }

    @RequestMapping("/select.do")
    @ResponseBody
    public JsonResult selectById(Integer id) {
        Employee employee = employeeDaoService.selectById(id);
        return new JsonResult(1, employee);
    }

    @RequestMapping("/selectE.do")
    @ResponseBody
    public JsonResult selectByEId(Integer id) {
        VEmployee employee = employeeDaoService.selectByEId(id);
        return new JsonResult(1, employee);
    }

    @RequestMapping("/delete.do")
    @ResponseBody
    public JsonResult deleteById(Integer id) {
        try {
            employeeDaoService.deleteById(id);
        } catch (Exception e) {
            return new JsonResult(0, e.getMessage());
        }
        return new JsonResult(1, "删除成功");
    }

    @RequestMapping("/addEmployee.do")
    @ResponseBody
    public JsonResult addById(VEmployee vEmployee) {
        try {
            System.out.println(vEmployee);
            employeeDaoService.add(vEmployee);
        } catch (Exception e) {
            return new JsonResult(0, e.getMessage());
        }
        return new JsonResult(1, "添加成功");
    }

    @RequestMapping("/update.do")
    @ResponseBody
    public JsonResult updateEById(VEmployee vEmployee,Integer id) {
        System.out.println(vEmployee);
        vEmployee.setId(id);
        employeeDaoService.updateEmployee(vEmployee);
        return new JsonResult(1, "修改成功");
    }
}
